package qimao.com.edu.servlet;

import java.io.File;
import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import qimao.com.edu.entity.Users;
import qimao.com.edu.service.UsersService;
import qimao.com.edu.service.impl.UsersServiceImpl;

@WebServlet("/register.do")
public class RegisterServlet extends HttpServlet{

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		
		UsersService us = new UsersServiceImpl();
		String username = req.getParameter("username");
		String password = req.getParameter("password");
		String nickname = req.getParameter("nickname");
		Users user = new Users();
		user.setUsername(username);
		user.setPassword(password);
		user.setNickname("读者");
		int result = us.registerUser(user);
		if(result == -1){
			req.setAttribute("msg","用户名已经存在");
			req.getRequestDispatcher("register.html").forward(req,resp);
		}else{
			resp.sendRedirect("login.html");
		}
		
	}
}
